package com.zikao.question.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.zikao.common.exception.RRException;
import com.zikao.common.service.RedisService;
import com.zikao.common.utils.MyMessage;
import com.zikao.question.entity.QuestionEntity;
import com.zikao.question.entity.Text2Entity;
import com.zikao.question.service.QuestionService;
import com.zikao.question.service.Text2Service;
import com.zikao.user.entity.UserEntity;
import com.zikao.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zikao.question.entity.AnswerEntity;
import com.zikao.question.service.AnswerService;
import com.zikao.common.utils.PageUtils;
import com.zikao.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;


/**
 * 
 *
 * @author likaisheng
 * @email 1805318928@qq.com
 * @date 2021-03-03 20:46:00
 */
@Slf4j
@Controller
@RequestMapping("question/answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = answerService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Long id){
		AnswerEntity answer = answerService.getById(id);

        return R.ok().put("answer", answer);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody AnswerEntity answer, HttpServletRequest request, HttpServletResponse response){
        UserEntity user = (UserEntity) request.getSession().getAttribute("user");
        answer.setUserId(user.getId());
        answerService.saveAnswer(answer);

        // 写入redis,未读消息通知
        // 创建消息
        MyMessage message = new MyMessage();
        UserEntity sender = (UserEntity) request.getSession().getAttribute("user");
        message.setSenderId(sender.getId());
        message.setSenderName(sender.getName());
        QuestionEntity question = questionService.getById(answer.getQuestionId());
        UserEntity receiver = userService.getById(question.getUserId());
        message.setReceiverId(receiver.getId());
        message.setReceiverName(receiver.getName());
        message.setType(2);
        message.setCreateTime(new Date());
        message.setUuid(UUID.randomUUID().toString());
        message.setReceiverEmail(receiver.getEmail());
        message.setContent("用户 <a>"+sender.getName()+"</a> 对你发布的问题 <a href='javascript:;' onclick='seeMessage(\""+message.getUuid()+"\","+question.getId()+")'>"+question.getTitle()+"</a> 进行了回答");
        redisService.sendMessage(message);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody AnswerEntity answer){
		answerService.updateById(answer);

        return R.ok();
    }

    /**
     * 采纳答案
     */
    @RequestMapping("/accept")
    @ResponseBody
    public R accept(@RequestBody AnswerEntity answer){
        try {
            QuestionEntity question = new QuestionEntity();
            question.setId(answer.getQuestionId());
            question.setStatus(1);
            questionService.updateById(question);

            answerService.updateById(answer);
        }catch (Exception e) {
            log.error("程序出现错误-accept", e);
            throw new RRException("程序出现错误，请联系管理员", 500);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Long[] ids){
		answerService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
